package com.ticket.web.controller.yanchu;

import com.ticket.common.annotation.Log;
import com.ticket.common.constant.Constants;
import com.ticket.common.core.controller.BaseController;
import com.ticket.common.core.domain.AjaxResult;
import com.ticket.common.core.domain.model.LoginUser;
import com.ticket.common.core.page.TableDataInfo;
import com.ticket.common.enums.BusinessType;
import com.ticket.common.utils.poi.ExcelUtil;
import com.ticket.framework.web.service.TokenService;
import com.ticket.system.domain.Yanchu;
import com.ticket.system.domain.vo.GroupCollectVo;
import com.ticket.system.domain.vo.YanchuCollectVo;
import com.ticket.system.service.IYanchuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 演出Controller
 * 
 * @date 2023-04-03
 */
@Api("场次登记")
@RestController
@RequestMapping("/system/yanchu")
public class YanchuController extends BaseController
{
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IYanchuService yanchuService;

    private static final Logger log = LoggerFactory.getLogger(YanchuController.class);

    /**
     * 查询演出列表
     */
    @ApiOperation("查询演出列表")
    @GetMapping("/list")
    public TableDataInfo list(Yanchu yanchu,HttpServletRequest request)
    {
        startPage();
        LoginUser user = tokenService.getLoginUser(request);
        yanchu.setCreateBy(user.getUserId()+"");
        if(null == yanchu.getState()){
            yanchu.setState(Constants.STATE_INIT);
        }
        List<Yanchu> list = yanchuService.selectYanchuList(yanchu);
        return getDataTable(list);
    }

    @ApiOperation("演出场次统计")
    @GetMapping("/listChangci")
    public Map listChangci()
    {
        List<YanchuCollectVo> list = yanchuService.selectYanchuCollectList();
        Map map = new HashMap();
        if(CollectionUtils.isNotEmpty(list)){
            map.put("total",list.stream().mapToInt(YanchuCollectVo::getValue).sum());
            map.put("list",list);
        }
        return map;
    }

    @ApiOperation("演出票数统计")
    @GetMapping("/listPiaoshu")
    public Map listPiaoshu()
    {
        List<YanchuCollectVo> list = yanchuService.selectYanchuPiaowuTotal();
        Map map = new HashMap();
        if(CollectionUtils.isNotEmpty(list)){
            map.put("total",list.stream().mapToInt(YanchuCollectVo::getValue).sum());
            map.put("list",list);
        }
        return map;
    }

    @ApiOperation("演出收入统计")
    @GetMapping("/listShouru")
    public Map listShouru()
    {
        List<YanchuCollectVo> list = yanchuService.selectYanchuShouruTotal();
        Map map = new HashMap();
        if(CollectionUtils.isNotEmpty(list)){
            String[] names = list.stream().map(vo->vo.getName()).collect(Collectors.toList()).toArray(new String[list.size()]);
            BigDecimal[] values = list.stream().map(vo->BigDecimal.valueOf(vo.getValue()).divide(BigDecimal.valueOf(10000)).setScale(2, RoundingMode.HALF_UP)).collect(Collectors.toList()).toArray(new BigDecimal[list.size()]);
            BigDecimal[] lirun = list.stream().map(vo->BigDecimal.valueOf(vo.getLirunVal()).divide(BigDecimal.valueOf(10000)).setScale(2, RoundingMode.HALF_UP)).collect(Collectors.toList()).toArray(new BigDecimal[list.size()]);
            BigDecimal[] chengben = list.stream().map(vo->BigDecimal.valueOf(vo.getChengbenVal()).divide(BigDecimal.valueOf(10000)).setScale(2, RoundingMode.HALF_UP)).collect(Collectors.toList()).toArray(new BigDecimal[list.size()]);
            if(null!=names){
                map.put("names",names);
            }

            if(null!=values){
                map.put("values",values);
            }
            if(null!=lirun){
                map.put("lirun",lirun);
            }
            if(null!=chengben){
                map.put("chengben",chengben);
            }

            map.put("totalLirun",list.stream().mapToInt(YanchuCollectVo::getLirunVal).sum());
            map.put("totalChengben",list.stream().mapToInt(YanchuCollectVo::getChengbenVal).sum());
            map.put("totalShouru",list.stream().mapToInt(YanchuCollectVo::getValue).sum());
        }
        return map;
    }

    /**
     * 查询部门汇总
     */
    @ApiOperation("查询部门汇总")
    @GetMapping("/listDept")
    public Map listDept(Yanchu yanchu,HttpServletRequest request)
    {
        startPage();
        if(null == yanchu.getDeptId() || 0 == yanchu.getDeptId()){
            LoginUser user = tokenService.getLoginUser(request);
            yanchu.setDeptId(user.getDeptId());
        }
        log.info("yanchu:{}",yanchu.getDateYanchu());
        List<Yanchu> list = yanchuService.selectYanchuList(yanchu);
        YanchuCollectVo yanchuCollectVo = yanchuService.selectYanchuListCollect(yanchu);
        Map map = new HashMap();
        map.put("list",getDataTable(list));
        map.put("yanchuCollectVo",yanchuCollectVo);
        return map;
    }
    /**
     * 集团领导查询
     */
    @ApiOperation("集团领导查询")
    @GetMapping("/listDeptGroup")
    public TableDataInfo listDeptGroup(Yanchu yanchu,HttpServletRequest request)
    {
        if(null==yanchu.getState()){
            yanchu.setState(Constants.STATE_PASS);
        }
        startPage();
        List<Yanchu> list = yanchuService.selectYanchuList(yanchu);
        return getDataTable(list);
    }

    /**
     * 查询集团汇总
     */
    @ApiOperation("查询集团汇总")
    @GetMapping("/listGroup")
    public TableDataInfo listGroup(Yanchu yanchu)
    {
        startPage();
        List<Yanchu> list = yanchuService.selectGroupList(yanchu);
        return getDataTable(list);
    }

    /**
     * 集团汇总导出
     */
    @Log(title = "集团汇总导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportGroupYanyi")
    public void exportGroupYanyi(HttpServletResponse response, Yanchu yanchu)
    {
        if(null==yanchu.getState()){
            yanchu.setState(Constants.STATE_PASS);
        }
        List<GroupCollectVo> list = yanchuService.selectGroupListRollup(yanchu);
        ExcelUtil<GroupCollectVo> util = new ExcelUtil<GroupCollectVo>(GroupCollectVo.class);
        util.exportExcel(response, list, "集团汇总");
    }

    /**
     * 导出演出列表
     */
    @Log(title = "演出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportYanyi")
    public void exportYanyi(HttpServletResponse response, Yanchu yanchu)
    {
        if(null==yanchu.getState()){
            yanchu.setState(Constants.STATE_PASS);
        }

        List<Yanchu> list = yanchuService.selectGroupYanchuList(yanchu);
        ExcelUtil<Yanchu> util = new ExcelUtil<Yanchu>(Yanchu.class);
        util.exportExcel(response, list, "场次登记数据");
    }

    /**
     * 导出部门演出列表
     */
    @Log(title = "演出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportDeptYanyi")
    public void exportDeptYanyi(HttpServletResponse response, Yanchu yanchu,HttpServletRequest request)
    {
        if(null==yanchu.getState()){
            yanchu.setState(Constants.STATE_PASS);
        }
        if(null == yanchu.getDeptId() || 0 == yanchu.getDeptId()){
            LoginUser user = tokenService.getLoginUser(request);
            yanchu.setDeptId(user.getDeptId());
        }
        List<Yanchu> list = yanchuService.selectGroupYanchuList(yanchu);
        ExcelUtil<Yanchu> util = new ExcelUtil<Yanchu>(Yanchu.class);
        util.exportExcel(response, list, "场次登记数据");
    }

    /**
     * 获取演出详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yanchuService.selectYanchuById(id));
    }

    /**
     * 新增演出
     */
    @PreAuthorize("@ss.hasPermi('system:yanchu:add')")
    @Log(title = "演出", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Yanchu yanchu, HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        yanchu.setCreateBy(loginUser.getUserId()+"");
        yanchu.setDeptId(loginUser.getDeptId());
        yanchu.setCreateDate(new Date());
        if(null==yanchu.getState()){
            yanchu.setState(Constants.STATE_INIT);
        }
        return toAjax(yanchuService.insertYanchu(yanchu));
    }

    /**
     * 修改演出
     */
    @PreAuthorize("@ss.hasPermi('system:yanchu:edit')")
    @Log(title = "演出", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Yanchu yanchu)
    {
        return toAjax(yanchuService.updateYanchu(yanchu));
    }

    /**
     * 删除演出
     */
    @PreAuthorize("@ss.hasPermi('system:yanchu:remove')")
    @Log(title = "演出", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yanchuService.deleteYanchuByIds(ids));
    }

    /**
     * 审核通过演出
     */
    @PreAuthorize("@ss.hasPermi('system:yanchu:remove')")
    @Log(title = "申报", businessType = BusinessType.UPDATE)
    @PostMapping("/shenbao/{ids}")
    public AjaxResult shenbao(@PathVariable Long[] ids)
    {
        return toAjax(yanchuService.shenbaoYanchuByIds(ids));
    }

    /**
     * 撤销申报
     */
    @Log(title = "取消申报", businessType = BusinessType.UPDATE)
    @PostMapping("/shenbaoCancel/{ids}")
    public AjaxResult shenbaoCancel(@PathVariable Long[] ids)
    {
        return toAjax(yanchuService.shenbaoCancelByIds(ids));
    }
}
